package com.jdrx.sw.sewage.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ExecutorPoolConfig {
    private static final Logger logger = LoggerFactory.getLogger(ExecutorPoolConfig.class);

    @Bean("asyncServiceExecutor")
    public ThreadPoolTaskExecutor asyncServiceExecutor() {
        logger.info("start asyncServiceExecutor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(6);
        //配置最大线程数
        executor.setMaxPoolSize(6);
        //配置队列大小
        executor.setQueueCapacity(999);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("async-service-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }

    @Bean("sewageJobExecutor")
    public ThreadPoolTaskExecutor sewageJobExecutor() {
        logger.info("start sewageJobExecutor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //ThreadPoolTaskExecutor executor = new VisiableThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(5);
        //配置最大线程数
        executor.setMaxPoolSize(10);
        //配置队列大小
        executor.setQueueCapacity(999);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("async-service-");
        // rejection-policy：当pool已经达到max size的时候，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }

//    @Bean
//    public Executor gysTasksExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("gysTasks-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
//
//    @Bean
//    public Executor gyTasksExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("gyTasks-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
//
//    @Bean
//    public Executor gjsTasksExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("gjsTasks-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
//
//    @Bean
//    public Executor deviceTimeDataFilterExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("deviceTimeDataFilter-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
//
//    @Bean
//    public Executor qualityReportTimeDataFilterExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("qualityReportTimeDataFilter-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
//
//    @Bean
//    public Executor energyReportTimeDataFilterExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("energyReportTimeDataFilter-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
//
//    @Bean
//    public Executor updateAlarmNumInitValueFilterExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setThreadNamePrefix("updateAlarmNumInitValueFilter-schedule-");
//        executor.setMaxPoolSize(5);
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(0);
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }
}